package edu.mtu.citizenscience.cslocationlestener;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.gpsservice.CSGpsService;
import edu.mtu.citizenscience.gpsservice.CSLocationListener;
import edu.mtu.citizenscience.gpsservice.CSLocationListenerConnection;
import edu.mtu.citizenscience.gpsservice.CSLocationListenerContainer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.support.v4.app.NavUtils;


/*
 * This activity is a demonstration of a main activity in app. 
 * It makes a binding to to the CSGpsService. 
 * Buttons can check that the connection is bound. 
 * Another button call the next activity, which is DisplayLocation.
 * 
 * Note that this demo uses AndroidQuery for making button and handling
 * http://code.google.com/p/android-query/
 * The library is found in vendor/
 * You will need to set Project -> Java Build Path -> Libraries -> add Jar
 * 
 */

public class StartUp extends Activity implements CSLocationListenerContainer {

	private static final String CLASS_NAME = "StartUp";
	private static final boolean DEBUG = true;
	
	/* Class for Location Listener Service */
	private CSLocationListener locationListener; // the service
	private CSLocationListenerConnection mConnection; // the connection
	
	private AQuery aq;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
        if(DEBUG) Log.d(CLASS_NAME, "onCreate");
        
        setContentView(R.layout.activity_start_up);
        
        /* Start binding */
        // make the binder-connection
        mConnection = new CSLocationListenerConnection(this);
        // make the intent
        Intent intent = new Intent(this, CSGpsService.class);
        // bind the service
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        /* Finished binding, but we don't have the location listener yet */
        
        this.aq = new AQuery(this); // the android-query library object  
        // Set up the buttons
        aq.id(R.id.check_connection_button).clicked(this, "onCheckConnectionClicked");
        aq.id(R.id.next_activity_button).clicked(this, "onNextActivityClicked");
        aq.id(R.id.close_down_button).clicked(this,"onCloseDownClicked");
    }
    public void onCloseDownClicked(View button)
    {
    	this.finish();
    }
    public void onNextActivityClicked(View button)
    {
    	// make the intent
    	Intent intent = new Intent(this, DisplayLocation.class);
    	// dispatch the intent
    	startActivity(intent);
    }
    public void onCheckConnectionClicked(View button)
    {
    	CharSequence text;
    	if(this.mConnection.isBound()) text = "is BOUND";
    	else text = "NOT bounded";
    	
    	Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
    	toast.show();
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible. container
        if(DEBUG) Log.d(CLASS_NAME, "onStart"); 
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
        if(DEBUG) Log.d(CLASS_NAME, "onResume");
        
        if(this.mConnection.isBound()) 
        {
        	Log.d("onResume", "bounded");
        	locationListener.setLooseUpdates();
        }
        else if(DEBUG) Log.d("onResume", "NOT bounded");         
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
        if(DEBUG) Log.d(CLASS_NAME, "onPause");
        
     // I am curious if bounded
        if(DEBUG){
        	if (this.mConnection.isBound()) Log.d("onDestroy", "bounded");
        	else Log.d("onDestroy", "NOT bounded");
        }    
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
        if(DEBUG) Log.d(CLASS_NAME, "onStop");

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
        if(DEBUG) Log.d(CLASS_NAME, "onDestroy");
        
        // I am curious if bounded
        if(DEBUG){
        	if (this.mConnection.isBound()) Log.d("onDestroy", "bounded");
        	else Log.d("onDestroy", "NOT bounded");
        }    
        unbindService(this.mConnection);
    }
    @Override
	public CSLocationListener getCSLocationListener() { return this.locationListener;}
    @Override
	public void setCSLocationListner(CSLocationListener locationListener) { this.locationListener = locationListener;}

} // end MainActivity
